package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GControlMode;
import g4p_controls.GDropList;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GOption;
import g4p_controls.GSlider;
import g4p_controls.GTextIconAlignBase;
import g4p_controls.GToggleControl;
import g4p_controls.GToggleGroup;
import g4p_controls.GValueControl;

import java.util.ArrayList;

import processing.core.PApplet;

public class TI_Controls_config extends PApplet {

	GOption opt0, opt1, opt2;
	GCheckbox cbx0;
	GLabel lbl0, lbl1;
	GButton btn0, btn1, btnResize;

	// Since all the classes for all these controls inherit 
	// from GTextIconControl this arraylist will keep them as
	// a collection so they can all be configured as a group
	// more easily
	ArrayList<GTextIconAlignBase> controls = new ArrayList<GTextIconAlignBase>();

	int bgcol = 240;

	public void setup(){
		size(560,600);
		G4P.setCursor(CROSS);
		// Get some sample text for later
		String[] lines = loadStrings("g4p/tisample.txt");
		String text = join(lines, '\n');
		// Options and checkboxes have default settings :
		//   left/middle text & left/middle icon alignment
		// Labels and buttons have default settings :
		//   centre/middle text & right/middle icon alignment
		// The buttons and labels will have there alignment 
		// changed to match the labels so they all start
		// matched to the drop lists.
		G4P.setCtrlMode(GControlMode.CENTER);
		lbl0 = new GLabel(this, 85, height/2, 150, 350, text);
		lbl0.setTextAlign(GAlign.LEFT, null);
		lbl0.tag = "GLabel text only";
		lbl0.tagNo = 999;
		G4P.setCtrlMode(GControlMode.CORNER);
		controls.add(lbl0);

		lbl1 = new GLabel(this, 200, 100, 140, 80, "Labels can also have icons like this one");
		lbl1.setIcon("g4p/bugs/bugtest.png", 1, GAlign.LEFT, null);
		lbl1.setTextAlign(GAlign.LEFT, null);
		lbl1.tag = "GLabel + icon only";
		controls.add(lbl1);

		btn0 = new GButton(this, 200, 190, 150, 80, "Buttons always have an opaque background");
		btn0.setTextAlign(GAlign.LEFT, null);
		btn0.tag = "GButton text only";
		controls.add(btn0);

		btn1 = new GButton(this, 200, 280, 150, 80, "As well as text buttons can also have icons");
		btn1.setIcon("g4p/smile.png", 3, GAlign.LEFT, null);
		btn1.setTextAlign(GAlign.LEFT, null);
		btn0.tag = "GButton text + icon";
		controls.add(btn1);

		opt0 = new GOption(this, 200, 10, 100, 18, "Option 0");
		tg.addControl(opt0);
		opt0.tag = "Option 0";
		controls.add(opt0);

		opt1 = new GOption(this, 200, 32, 100, 18, "Option 1");
		tg.addControl(opt1);
		opt1.tag = "Option 1";
		controls.add(opt1);

		opt2 = new GOption(this, 200, 54, 100, 18, "Option 2");
		tg.addControl(opt2);
		opt2.tag = "Option 2";
		controls.add(opt2);

		opt0.setSelected(true);

		cbx0 = new GCheckbox(this, 200, 76, 100, 20, "Tick box");
		opt0.tag = "GCheckbox 0";
		controls.add(cbx0);

		makeTextIconConfigControls();
	}

	public void draw(){
		background(bgcol);
		fill(227,230,255);
		noStroke();
		rect(width - 190, 0, 200, height);
	}

	public void keyTyped(){
		if(key == 'l')
			System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		if(key == 'u')
			lbl0.forceBufferUpdate();
	}
	public void handleSliderEvents(GValueControl slider, GEvent event) { 
		if(slider == sdrBack)
			bgcol = slider.getValueI();	
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) { 
		if(knbAngle == knob)
			for(GTextIconAlignBase control : controls)
				control.setRotation(knbAngle.getValueF(), GControlMode.CENTER);
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000){
			for(GTextIconAlignBase control : controls)
				control.setLocalColorScheme(button.tagNo - 1000);
		}
		if(button == btn0){
//			lbl0.setIcon("g4p/btnA.png", 1, GAlign.LEFT, GAlign.MIDDLE);
			lbl0.setIcon("g4p/ghost0.png", 1, GAlign.LEFT, GAlign.MIDDLE);
		}
		if(button == btn1){
			opt0.setHeightToFit();
			opt1.setHeightToFit();
			opt2.setHeightToFit();
			cbx0.setHeightToFit();
			G4P.setCtrlMode(GControlMode.CENTER);
			lbl0.setHeightToFit();
			G4P.setCtrlMode(GControlMode.CORNER);
			lbl1.setHeightToFit();
			btn0.setHeightToFit();
			btn1.setHeightToFit();
		}
	}


	public void handleToggleControlEvents(GToggleControl option, GEvent event) {
		if(option == optPlain)
			for(GTextIconAlignBase control : controls)
				control.setTextPlain();
		else if(option == optBold)
			for(GTextIconAlignBase control : controls) {
				control.setTextPlain();
				control.setTextBold();
			}
		else if(option == optItalic)
			for(GTextIconAlignBase control : controls) {
				control.setTextPlain();
				control.setTextItalic();
			}
		else if(option == optItalic)
			for(GTextIconAlignBase control : controls) {
				control.setTextPlain();
				control.setTextItalic();
			}
		else if(option == optBoldItalic)
			for(GTextIconAlignBase control : controls) {
				control.setTextBold();
				control.setTextItalic();
			}
		else if(option == cbxOpaque)
			for(GTextIconAlignBase control : controls)
				control.setOpaque(cbxOpaque.isSelected());
	}

	public void handleDropListEvents(GDropList list, GEvent event) {
		GAlign na = GAlign.getFromText(list.getSelectedText());
		if(list == textH)
			for(GTextIconAlignBase control : controls)
				control.setTextAlign(na, null);
		if(list == textV)
			for(GTextIconAlignBase control : controls)
				control.setTextAlign(null, na);
		if(list == iconH)
			for(GTextIconAlignBase control : controls)
				control.setIconAlign(na, null);
		if(list == iconV){
			for(GTextIconAlignBase control : controls)
				control.setIconAlign(null, na);
		}
	}


	// The code in this tab is used to create all the other
	// controls needed to configure the knob control.

	GToggleGroup tg = new GToggleGroup();
	GOption optPlain, optBold, optItalic, optBoldItalic;
	GCheckbox cbxOpaque;
	GSlider sdrBack;
	GLabel lblTestStyle, lblVisible, lblNbrTicks, lblEasing;
	GLabel lblTextA, lblIconA;
	GKnob knbAngle;
	GButton[] btnColours = new GButton[8];
	GDropList textH, textV, iconH, iconV;

	public void makeTextIconConfigControls(){
		// Create colour scheme selectors
		int x = width - 42, y = 2;
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, x, y + i * 20, 40, 18, "" + (i+1));
			btnColours[i].tag = "Button: " + (i+1);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000+i;
		}	
		// Create sliders
		x = width-100; y = 162;
		sdrBack = new GSlider(this, x, y, 162, 80, 12);
		sdrBack.setLimits(bgcol, 0, 255);
		sdrBack.setRotation(-PI/2);
		sdrBack.setTextOrientation(G4P.ORIENT_RIGHT);
		sdrBack.setEasing(20);
		sdrBack.setShowValue(true);
		sdrBack.setShowTicks(true);

		x = width - 180; y = 2;
		lblVisible = new GLabel(this, x, y, 80, 18, "Visible");
		lblVisible.setTextBold();
		cbxOpaque = new GCheckbox(this, x, y + 20 , 70, 18, "Opaque");

		x = width - 180; y = 52;
		lblTestStyle = new GLabel(this, x, y, 80, 18, "Text Style");
		//		lblTestStyle.setTextAlign(GAlign.LEFT);
		lblTestStyle.setTextBold();
		optPlain = new GOption(this, x, y + 20, 80, 18, "Plain");
		optBold = new GOption(this, x, y + 40, 80, 18, "Bold");
		optItalic = new GOption(this, x, y + 60, 80, 18, "Italic");
		optBoldItalic = new GOption(this, x, y + 80, 80, 18, "Bold-Italic");

		tg.addControls(optPlain, optBold, optItalic, optBoldItalic);
		optPlain.setSelected(true);

		// Allignment droplists
		String[] items;	
		x = width - 180; y = 170;
		lblTextA = new GLabel(this, x, y, 80, 38, "Text Alignment");
		lblTextA.setTextBold();

		items = new String[] { "LEFT", "CENTER", "RIGHT", "JUSTIFY"};
		textH = new GDropList(this, x, y + 40, 80, 90, 4);
		textH.setItems(items, 0);
		textH.tag = "textH";

		items = new String[] { "TOP", "MIDDLE", "BOTTOM"};
		textV = new GDropList(this, x, y + 60, 80, 90, 4);
		textV.setItems(items, 1);
		textV.tag = "textV";

		x = width - 90; y = 170;
		lblIconA = new GLabel(this, x, y, 80, 38, "Icon Alignment");
		lblIconA.setTextBold();

		items = new String[] { "LEFT", "RIGHT"};
		iconH = new GDropList(this, x, y + 40, 80, 90, 4);
		iconH.setItems(items, 0);
		iconH.tag = "iconH";

		items = new String[] { "TOP", "MIDDLE", "BOTTOM"};
		iconV = new GDropList(this, x, y + 60, 80, 90, 4);
		iconV.setItems(items, 1);
		iconV.tag = "iconV";

		x = width - 130; y = height - 80;
		knbAngle = new GKnob(this, x, y, 70, 70, 0.6f);
		knbAngle.setTurnRange(0, 360);
		knbAngle.setLimits(0.0f, 0.0f, TWO_PI);
		knbAngle.setTurnMode(G4P.CTRL_ANGULAR);
		knbAngle.setIncludeOverBezel(true);
		knbAngle.setNbrTicks(13);
		knbAngle.setStickToTicks(true);

	}
}
